import app from '@/app'
import { checkout, checkoutToken, checkoutUpdate, checkoutClean } from '@/api/manage/user'

import base from '@/app/model'

// 继承总模型
const model = new base;

model.get = () => {
  return new Promise((resolve, jeject) => {
    app.ui.loading()
    checkout().then(res => {
      model.setList(res)
      // 我的设备
      model.setData({ my: app.tokenCheckout() })
      resolve(res)
    }).catch(res => {
      jeject(res)
    }).finally(() => {
      app.ui.loadingClose()
    })
  })
}

// 绑定设备
model.token = (...arge) => {
  return new Promise((resolve, jeject) => {
    checkoutToken(...arge).then(res => {
      model.get()
      // 绑定设备
      app.tokenCheckout(res.token)
      app.ui.message('success', 'ok')
      resolve(res)
    }).catch(res => {
      app.ui.message('error', 'error.' + res.message)
      jeject(res)
    })
  })
}

model.update = (...arge) => {
  return new Promise((resolve, jeject) => {
    checkoutUpdate(...arge).then(res => {
      model.get()
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      app.ui.message('error', 'error.' + res.message)
      jeject(res)
    })
  })
}

// 解绑
model.clean = id => {
  return new Promise((resolve, jeject) => {
    checkoutClean(id).then(res => {
      model.get()
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      app.ui.message('error', 'error.' + res.message)
      jeject(res)
    })
  })
}

export default model